# Date: 2013-12-30
php_modules_install() {

logs "Info" "Install PDO_MYSQL"

mkdir /usr/local/php/etc/php.d
tar -zxf PDO_MYSQL-1.0.2.tar.gz
cd PDO_MYSQL-1.0.2
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make && make install
cat >> /usr/local/php/etc/php.d/pdo.ini << EOF
[PDO_MySQL]
extension = "/usr/local/php/lib/php/extensions/pdo_mysql.so"
EOF
cd ..

logs "Info" "Install eaccelerator"

tar -jxf eaccelerator-0.9.6.1.tar.bz2 
cd eaccelerator-0.9.6.1
/usr/local/php/bin/phpize 
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make && make install
mkdir -p /var/cache/eaccelerator_cache
cat >> /usr/local/php/etc/php.d/eaccelerator.ini << EOF
[eaccelerator] 
;zend_extension=
extension="/usr/local/php/lib/php/extensions/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/var/cache/eaccelerator_cache" 
eaccelerator.enable="1"
eaccelerator.optimizer="1" 
eaccelerator.check_mtime="1" 
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0" 
eaccelerator.shm_ttl="3600" 
eaccelerator.shm_prune_period="3600" 
eaccelerator.shm_only="0"
eaccelerator.compress="1" 
eaccelerator.compress_level="9"
EOF
if [ `cat /proc/sys/kernel/shmmax` -lt 67108864 ] ; then
   grep "^kernel.shmmax" /etc/sysctl.conf >/dev/null 2>&1
   if [ "$?" = "0" ] ; then
      sed -i 's/kernel.shmmax/#kernel.shmmax/g' /etc/sysctl.conf
   fi
   echo "kernel.shmmax = 128000000" >> /etc/sysctl.conf
   sysctl -p
fi
cd ..

logs "Info" "Install Zend"

sed -i "s#;always_populate_raw_post_data = On#always_populate_raw_post_data = On#g" /usr/local/php/etc/php.ini 
sed -i 's#output_buffering = Off#output_buffering = On#'  /usr/local/php/etc/php.ini
sed -i 's/post_max_size = 8M/post_max_size = 20M/g' /usr/local/php/etc/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 20M/g' /usr/local/php/etc/php.ini
sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/etc/php.ini
sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php/etc/php.ini
sed -i 's/; cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
sed -i 's/; cgi.fix_pathinfo=0/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/etc/php.ini
sed -i 's/expose_php = On/expose_php = Off/g' /usr/local/php/etc/php.ini
echo 'extension_dir = "/usr/local/php/lib/php/extensions"' >> /usr/local/php/etc/php.ini

cd $CWD
if [ "$phpv" = "5.3" ] ; then
    if [ "$bit" = "x64" ]; then
        tar -zxf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
        cp ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so /usr/local/php/lib/php/extensions/
    else
        tar -zxf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
        cp ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/ZendGuardLoader.so /usr/local/php/lib/php/extensions/
    fi
cat >> /usr/local/php/etc/php.d/zend.ini << EOF
[Zend Guard Loader]
zend_loader.enable = 1
zend_extension = "/usr/local/php/lib/php/extensions/ZendGuardLoader.so"
zend_loader.disable_licensing = 0
zend_loader.obfuscation_level_support = 3
;zend_loader.license_path =
EOF
else
    if [ "$bit" = "x64" ]; then
        tar -zxvf ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz
        cp ZendOptimizer-3.3.9-linux-glibc23-x86_64/data/5_2_x_comp/ZendOptimizer.so /usr/local/php/lib/php/extensions/
    else
        tar -zxvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
        cp ZendOptimizer-3.3.9-linux-glibc23-i386/data/5_2_x_comp/ZendOptimizer.so /usr/local/php/lib/php/extensions/
    fi
cat >> /usr/local/php/etc/php.d/zend.ini << EOF
[Zend Optimizer] 
zend_optimizer.optimization_level = 1
zend_optimizer.encoder_loader = 0
zend_extension = "/usr/local/php/lib/php/extensions/ZendOptimizer.so"
EOF
fi

phpexdir=`ls /usr/local/php/lib/php/extensions | grep no-debug`
mv /usr/local/php/lib/php/extensions/$phpexdir/* /usr/local/php/lib/php/extensions/
mkdir -p /usr/local/php/var/run && mkdir /usr/local/php/var/log
chown -R www:www /usr/local/php/var
}